using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace LeisureCenter.Tools
{
    public static class Utils
    {
        [DllImport("user32.dll")]
        static extern bool ClientToScreen(IntPtr hwnd, ref Point lpPoint);

        public static Rectangle GetControlScreenBounds(Control control)
        {
            if (control != null)
            {
                Point point = control.Location;
                ClientToScreen(control.Parent.Handle, ref point);
                return new Rectangle(point, control.Size);
            }
            return new Rectangle();
        }

        public static string FormatSize(long fileSize)
        {
            const double dwKB = 1024;          // Kilobyte
            const double dwMB = 1024 * dwKB;   // Megabyte
            const double dwGB = 1024 * dwMB;   // Gigabyte

            if (fileSize < dwKB)
                return fileSize.ToString("N") + " B";
            else
                if (fileSize < dwMB)
                    return ((double)fileSize / dwKB).ToString("N") + " KB";
                else
                    if (fileSize < dwGB)
                        return ((double)fileSize / dwMB).ToString("N") + " MB";
                    else
                        //if (fileSize >= dwGB)
                            return ((double)fileSize / dwGB).ToString("N") + " GB";
        }
    }
}
